 <template>
  <div class="hm_article">
    <header>
      <!-- 阻止a标签的默认行为  javascript:;-->
      <div class="link">
        <a href="javascript:;" @click="changeSorter('weight_desc')">推荐</a>
        <a href="javascript:;" @click="changeSorter(null)">最新</a>
      </div>
      <div class="logo"><img src="@/assets/logo.dbd40807.png" alt="" /></div>
    </header>

    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></ArticleItem>
      </van-list>
      <p>刷新次数: {{ count }}</p>
    </van-pull-refresh>
    
  </div>
</template>

<script>
import { getArticleList } from "@/utils/request";
import { getUserInfo } from "@/utils/request";
import { Toast } from "vant";
export default {
  name: "HmArticle",
  data() {
    return {
      list: [],
      current: 1,
      sorter: "weight_desc", //最新
      pageSize: 10,
      loading: false,
      finished: false,
      count: 0,
      isLoading: false,
    };
  },
  methods: {
    
    async onLoad() {
      // 发送请求,获取文章列表
      const res = await getArticleList({
        current: this.current,
        sorter: this.sorter,
      });
      // console.log("123", res);
      this.loading = false; // 请求结束,关闭加载中
      this.list.push(...res.data.rows); // 追加数据
      this.current++; // 页码+1
      if (res.data.rows.length < 10) {
        this.finished = true; // 没有更多数据
        
      }
    },
    changeSorter(sorter) {
      // 切换排序
      this.sorter = sorter;
      // 清空数据
      this.list = [];
      this.finished = false;
      this.loading = false;
      // 重置页码
      this.current = 1;

      // 重新获取数据
      this.loading = true;
      this.onLoad();
    },
    async getUserInfo() {
      const res = await getUserInfo();
      console.log(res);
    },
    onRefresh() {
      setTimeout(() => {
        Toast('刷新成功');
        this.isLoading = false;
        this.count++;
      }, 1000);
    },
  },


};
</script>

<style lang="less" scoped>
.hm_article {
  header {
    height: 44px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
    .link {
      display: flex;
      a {
        display: block;
        height: 44px;
        line-height: 44px;
        margin-left: 20px;
        color: #999;
        font-size: 14px;
        text-decoration: none;
        &:hover {
          color: #222;
          border-bottom: 1px solid #222;
        }

        &.active {
          color: #999;
        }
      }
    }
    .logo {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      img {
        display: block;
        width: 60px;
        height: 30px;
        margin-left: 10px;
      }
    }
  }
}
</style>
